﻿@{
    Layout = "~/Areas/Web/Views/Shared/_UserCenter.cshtml";
    ViewBag.Title = "我的订单";
}
<div class="coverd cover" style="display:none;"></div>
@model IEnumerable<Himall.Web.Areas.Web.Models.OrderListModel>
@using Himall.Core
<script src="~/Scripts/datetimeCustom.js"></script>

<div class="order-dialog" style="display:none;">
    <div class="close" onclick="closeQR();"><img src="/Images/close_01.png"></div>
    <h3>恭喜您获得<span></span>个红包</h3>
    <h5>快去把它分享给好友吧！</h5>
    <img style="width:180px;height:180px;" src="/Images/close_01_13.jpg" >
    <p>请使用微信扫描该二维码，在打开的页面选择分享到朋友圈</p>
</div>


<div class="box1 lh24">

    <div class="title">
        <span class="title_txt cur">我的订单</span>
    </div>
    <div class="search-area">
        <label>关键字：</label>
        <input class="search_order" id="txtkeyword" type="text" value="商品名称、订单编号" />
        <label>开始日期：</label>
        <input id="inputStartDate" class="search_order start_datetime" type="text">

        <label>结束日期：</label>
        <input id="inputEndDate" class="search_order end_datetime" type="text">
        <button class="search_btn pointer" onclick="Search()">搜索</button>
    </div>
    <div class="border-box">

        <table class="tb-void">
            <colgroup>
                <col width="1" />
                <col width="330" />
                <col width="90" />
                <col width="120" />
                <col width="140" />
                <col width="120" />
                <col width="150" />
                <col width="1" />
            </colgroup>
            <thead>
                <tr class="tr">
                    <th colspan="2">订单信息</th>
                    <th>收货人</th>
                    <th>订单金额</th>
                    <th>
                        <select name="" id="submitDate" onchange="Query()">
                            <option selected="selected" value="all">请选择...</option>
                            <option value="threeMonth">最近三个月</option>
                            <option value="halfyear">最近半年</option>
                            <option value="year">最近一年</option>
                            <option value="yearAgo">一年以前</option>
                        </select>
                    </th>
                    <th>
                        @Html.DropDownList( "status" , @Himall.Model.OrderInfo.OrderOperateStatus.Close.ToSelectList() , new { onchange = "Query()" } )
                    </th>
                    <th colspan="2">操作</th>
                </tr>
            </thead>
            <tbody id="tb-1877769835">
                @if( Model.Count() == 0 )
                {
                    <tr><td colspan="8"><div class="empty"><b></b>暂时没有任何的订单信息！</div></td></tr>
                }
                @{var before = true;
                var child = false;}
                @foreach( var m in Model )
                {
                    var model = Model.Where( a => a.OrderDate == m.OrderDate ).ToArray();
                    if( model.Count() > 1 && m.Id == model[ 0 ].Id )
                    {
                        child = true;
                        <!--<tr class="tr-th-info">
                            <td colspan="6">
                                <span class="tcol">
                                    尊敬的客户，由于你的商品属于不同商家，所以你的订单被拆分为多个子订单分开配送，给你带来的不便还请谅解。
                                </span>
                            </td>
                        </tr>-->
                    }
                    else if( model.Count() > 1 )
                    {
                        child = true;
                    }
                    else
                    {
                        child = false;
                    }
                    <tr class="sep-row"><td colspan="8"></td></tr>
                    <tr class="tr-th">
                        <td class="sep-col"></td>
                        <td class="tr-th-text" colspan="6">
                            <span class="tcol1">
                                订单编号:
                                <a href="@Url.Action("detail", new { id=@m.Id})">
                                    @m.Id @if( m.UserId != ViewBag.UserId )
                                    {<label>(子订单)</label>}
                                </a>
                                @if( m.ReceiveBonus != null )
                                {
                                    @*<img src="@m.ReceiveBonus.BonusQR" />*@
                                    <img style="cursor: pointer;" src="/Images/myorder_bonus.jpg" onclick="bonusQR( '@m.ReceiveBonus.BonusQR' , @m.ReceiveBonus.Himall_ShopBonus.Count );">
                                }

                            </span>


                            <span><a target="_blank" href="@Url.Action("home", "shop", new { id=@m.ShopId})">@m.ShopName</a></span>
                            <span class="tcol3">
                                @Html.Action( "CustmerServices" , new { shopId = @m.ShopId } )
                            </span>
                            <span class="tcol3 hl_blue">@(m.OrderType.HasValue ? m.OrderType.Value.ToDescription() + "订单" : "")</span>
                        </td>
                        <td class="sep-col"></td>
                    </tr>
                    <tr class="tr-td" oty="0,4" id="track1877769835">
                        <td class="sep-col l"></td>
                        <td>
                            <div class="img-list">
                                @foreach( var item in m.OrderItemList )
                                {
                                    var spc = "";
                                    if( !string.IsNullOrWhiteSpace( item.Color ) ) { spc += "颜色：" + item.Color; }
                                    if( !string.IsNullOrWhiteSpace( item.Size ) ) { spc += "，尺寸：" + item.Size; }
                                    if( !string.IsNullOrWhiteSpace( item.Version ) ) { spc += "，版本：" + item.Version; }
                                    <div>
                                        <a target="_blank" class="img-box" href="@Url.Action("Detail", "Product", new { id = @item.ProductId})" style="float:none;">
                                            <img width="50" height="50" title="@item.ProductName 【@spc.TrimStart('，')】" src="@item.ThumbnailsUrl" class="err-product" />
                                        </a>
                                        <span class="ftx-04">@item.SalePrice</span>
                                        <span>x @item.Quantity</span>
                                        <div class="icon-img">
                                            @if( item.CashDepositsObligation.IsCustomerSecurity )
                                            {
                                                <img src="/Images/Security.jpg" />
                                            }
                                            @if( item.CashDepositsObligation.IsSevenDayNoReasonReturn )
                                            {
                                                <img src="/Images/SevenDay.jpg" />
                                            }
                                            @if( item.CashDepositsObligation.IsTimelyShip )
                                            {
                                                <img src="/Images/TimelyDelivery.jpg" />
                                            }
                                        </div>
                                    </div>
                                }
                            </div>
                        </td>
                        <td><div class="u-name">@m.ShipTo</div></td>
                        <td>
                            <span class="ftx-04"> ￥@m.OrderTotalAmount.ToString( "F2" ) </span>

                            <br />
                            @m.PaymentTypeName
                        </td>
                        <td>
                            <span class="ftx-03">@m.OrderDate</span>
                            <input type="hidden" value="@m.OrderDate" />
                        </td>
                        <td class="td-01">
                            <strong state="ddfk" class="ftx-04 order-statu">@m.OrderStatus.ToDescription()</strong>
                        </td>

                        <td width="100" class="order-doi">
                            @if( m.UserId == ViewBag.UserId )
                            {
                                if( m.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitReceiving )
                                {
                                    <a href="javascript:;" onclick="ConfirmOrder('@m.Id')" class="btn btn-4">确认收货</a><br />
                                }
                                if( m.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.Finish && m.OrderCommentInfo.Count == 0 )
                                {
                                    <a href="@Url.Action("index", "OrderEvaluation", new { id = @m.Id })"><s></s>评价</a>
                                }

                            }
                            @if( m.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitPay )
                            {
                                <a href="@Url.Action("Pay", "Order", new { orderIds=@m.Id})" target="_blank" class="btn btn-4"><s></s>付&nbsp;款</a><br />
                            }
                            <a href="@Url.Action("Detail", new { id=@m.Id})">查看</a><br />
                            @if( m.UserId == ViewBag.UserId )
                            {
                                bool isShowRefund = false;
                                bool iscanRefund = false;
                                if( m.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.Finish )
                                {
                                    isShowRefund = true;
                                    if( m.FinishDate.Value.AddDays( ViewBag.SalesRefundTimeout ) > DateTime.Now )
                                    {
                                        iscanRefund = true;
                                    }
                                }
                                if( m.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitReceiving )
                                {
                                    isShowRefund = true;
                                    iscanRefund = true;
                                }

                                if( isShowRefund )
                                {
                                    if( iscanRefund )
                                    {
                                        <a href="@Url.Action("index", "OrderRefund", new { keywords = @m.Id })">退货/退款</a><br />
                                    }
                                    else
                                    {
                                        <a href="#" onclick="RefundProductTip()">申请返修/退换货</a><br />
                                    }
                                }

                                if( @m.OrderStatus == @Himall.Model.OrderInfo.OrderOperateStatus.WaitPay )
                                {
                                    <a href="javascript:;" onclick="CancelOrder('@m.Id')">取消订单</a><br />
                                }
                                if( @m.OrderStatus == @Himall.Model.OrderInfo.OrderOperateStatus.WaitDelivery )
                                {
                                    switch( m.RefundStats )
                                    {
                                        case null:
                                        case 0:
                                            <a href=@Url.Action("RefundApply", "OrderRefund", new { id = @m.Id })>订单退款</a><br />
                                            break;
                                        case 4:
                                        <span>退款被拒绝</span><br />
                                            break;
                                        default:
                                        <span>退款中</span><br />
                                            break;
                                    }
                                }
                                var sku = string.Join( "," , m.OrderItemList.Select( a => a.SkuId ) );
                                var num = string.Join( "," , m.OrderItemList.Select( a => a.Quantity ) );

                                if( m.OrderStatus != Himall.Model.OrderInfo.OrderOperateStatus.WaitPay && m.OrderStatus != Himall.Model.OrderInfo.OrderOperateStatus.Close )
                                {
                                    <a href="/cart/BatchAddToCart?skuids=@sku&counts=@num" target="_blank"><s></s>还要买</a>
                                }
                            }
                        </td>
                        <td class="sep-col r"></td>
                    </tr>
                }


            </tbody>
        </table>
        <div class="mt10">
            <div id="pagin-btm" class="pagin fr">
                @{
                    var count = ( ( PagingInfo )ViewBag.pageInfo ).TotalPages;
                    var curr = ( ( PagingInfo )ViewBag.pageInfo ).CurrentPage;
                    var keywords = Request.QueryString[ "keywords" ];
                    var orderStatus = Request.QueryString[ "orderStatus" ];
                    var orderDate = Request.QueryString[ "orderDate" ];
                    var purchaser = Request.QueryString[ "purchaser" ];
                    var startDate = Request.QueryString[ "startDateTime" ];
                    var endDate = Request.QueryString[ "endDateTime" ];
                }
                @Html.PageLinks( ( PagingInfo )ViewBag.pageInfo , x => Url.Action( "Index" , new { pageNo = x , keywords = keywords , orderStatus = orderStatus , orderDate = orderDate , startDateTime = startDate , endDateTime = endDate , purchaser = purchaser } ) )
                <span class="page-skip">
                    <em>&nbsp;&nbsp;共 @count 页&nbsp;&nbsp;&nbsp;&nbsp;</em>
                </span>
            </div>

            <div class="clr"></div>
        </div>
    </div>

</div>

<script>

    $( function ()
    {
        @if( ViewBag.Grant != null )
        {
            <text>
        bonusQR( '@ViewBag.Grant.BonusQR' , @ViewBag.Grant.Himall_ShopBonus.Count );

        </text>
        }
    } )
    function Query()
    {
        // var keywords = $("#txtkeyword").val();
        var orderDate = $( "#submitDate" ).val();
        var orderStatus = $( "#status" ).val();
        window.location.href = "?orderDate=" + orderDate + "&orderStatus=" + orderStatus;
    }


    function ConfirmOrder( orderId )
    {
        $.dialog.confirm( "你确定收到货了吗？", function () { Confirm( orderId ); } );
    }
    function RefundProductTip()
    {
        $.dialog.alert( "很遗憾，该维权入口已经关闭<br/>1、交易已过退货维权受理期,您无法发起退换货<br/>2、请在帮助中心查看平台退换货流程", '', 2000 );
    }
    function Confirm( orderId )
    {
        var loading = showLoading();
        $.ajax( {
            type: 'post',
            url: "@Url.Action("ConfirmOrder")",
            dataType: 'json',
            data: { orderId: orderId },
            success: function ( d )
            {
                loading.close();
                if ( d.success )
                {
                    $.dialog.succeedTips( "确认成功！", function ()
                    {
                        window.location.href = window.location.href;
                    }, 1 );
                }
                else
                {
                    $.dialog.errorTips( "确认失败！", '', 2 );
                }
            }
        } );
    }

    function CancelOrder( orderId )
    {
        $.dialog.confirm( "确定取消该订单吗？", function () { Cancel( orderId ); } );
    }
    function Cancel( orderId )
    {
        var loading = showLoading();
        $.ajax( {
            type: 'post',
            url: "@Url.Action("CloseOrder")",
            dataType: 'json',
            data: { orderId: orderId },
            success: function ( d )
            {
                loading.close();
                if ( d.success )
                {
                    $.dialog.succeedTips( "取消成功！", function ()
                    {
                        window.location.href = window.location.href;
                    }, 1 );
                }
                else
                {
                    $.dialog.errorTips( "取消失败！", '', 2 );
                }
            }
        } );
    }

    function Search()
    {
        var keywords = $( "#txtkeyword" ).val();
        if ( keywords == "商品名称、订单编号" )
            keywords = "";
        var purchaser = $( "#purchaser" ).val();
        var startDate = $( "#inputStartDate" ).val();
        var endDate = $( "#inputEndDate" ).val();
        window.location.href = "?keywords=" + keywords + '&startDateTime=' + startDate + '&endDateTime=' + endDate + '&purchaser=' + purchaser;
    }



    $( function ()
    {
        document.onkeydown = function ( e )
        {
            var ev = document.all ? window.event : e;
            if ( ev.keyCode == 13 )
            {
                var keywords = $( "#txtkeyword" ).val();
                if ( keywords == "商品名称、订单编号" )
                    keywords = "";
                var purchaser = $( "#purchaser" ).val();
                var startDate = $( "#inputStartDate" ).val();
                var endDate = $( "#inputEndDate" ).val();
                window.location.href = "?keywords=" + keywords + '&startDateTime=' + startDate + '&endDateTime=' + endDate + '&purchaser=' + purchaser;
            }
        }

        $( '#txtkeyword' ).focus( function ()
        {
            var a = $( this ).val(),
                b = '商品名称、订单编号';
            if ( a == b )
            {
                $( this ).val( '' ).css( 'color', '#333' );
            }
        } ).blur( function ()
        {
            var a = $( this ).val(),
                b = '商品名称、订单编号';
            if ( !a )
            {
                $( this ).val( b ).css( 'color', '#999' );
            }
        } );
        var orderStatus = "@Request.QueryString["orderStatus"]";
        var orderDate = "@Request.QueryString["orderDate"]";
        var keywords = html_decode( "@Request.QueryString["keywords"]" );
        var startDate = "@Request.QueryString["startDateTime"]";
        var endDate = "@Request.QueryString["endDateTime"]";
        if ( orderDate != "" && orderDate != null )
            $( "#submitDate" ).val( orderDate );
        if ( orderStatus != "" && orderStatus != null )
            $( "#status" ).val( orderStatus );
        if ( keywords != "" && keywords != null )
            $( "#txtkeyword" ).val( keywords );
        if ( startDate != '' && startDate != null )
            $( "#inputStartDate" ).val( startDate );
        if ( endDate != '' && endDate != null )
            $( "#inputEndDate" ).val( endDate );
    } );

    function html_decode( str )
    {
        var s = "";
        if ( str.length == 0 ) return "";
        s = str.replace( /&amp;/g, "&" );
        s = s.replace( /&lt;/g, "<" );
        s = s.replace( /&gt;/g, ">" );
        s = s.replace( /&nbsp;/g, " " );
        s = s.replace( /&#39;/g, "\'" );
        s = s.replace( /&quot;/g, "\"" );
        s = s.replace( /<br\/>/g, "\n" );
        return s;
    }

    function bonusQR( path, count )
    {

        $( ".order-dialog > img" )[0].src = path;
        $( ".order-dialog h3 span" ).html( count );
        $( ".order-dialog" ).css( "display", "block" );
        $( ".coverd" ).css( "display", "block" );
    }

    function closeQR()
    {
        $( ".coverd" ).css( "display", "none" );
        $( ".order-dialog" ).css( "display", "none" );
    }
</script>